﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

/********************************************************************
 * 作者: Ruiky
 * 创建日期：2013-07-07
 * 功能：公共函数类,主要处理各种数据类型的转换
 ********************************************************************/
namespace NanoERP.Common
{
    public class Normal
    {
        /// <summary>
        /// 移除小数点后多余的小数零
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string TrimNum(string str)
        {
            if (str == "" || str == "0") return "0";
            if (str.IndexOf(".", 0, str.Length) < 0) return str;

            for (int i = str.Length; i >= 0; i--)
            {
                if (str.Substring(i - 1, 1) == "0")
                    str = str.Substring(0, i - 1);
                else
                    break;
            }

            if (str.Substring(str.Length - 1, 1) == ".")
                str = str.Substring(0, str.Length - 1);

            return str;
        }

        /// <summary>
        /// 移除所有空白字符
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TrimAll(object obj)
        {
            string result = Regex.Replace(ToString(obj), @"\s+", "");
            return result;
        }

        /// <summary>
        /// 类型转换String(默认)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(object obj)
        {
            return obj == null ? "" : obj.ToString();
        }

        /// <summary>
        /// 类型转换Int(默认)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(object obj)
        {
            int result = 0;
            try
            {
                result = int.Parse(obj.ToString());
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 类型转换Long(默认)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long ToLong(object obj)
        {
            long result = 0;
            try
            {
                result = long.Parse(obj.ToString());
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 类型转换Float(默认)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static float ToFloat(object obj)
        {
            float result = 0;
            try
            {
                result = float.Parse(obj.ToString());
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 类型转换Decimal(默认)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object obj)
        {
            decimal result = 0;
            try
            {
                result = decimal.Parse(obj.ToString());
            }
            catch
            {
                result = 0;
            }
            return result;
        }

        /// <summary>
        /// 类型转换为Byte[]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] ToByte(object obj)
        {
            return obj == null ? new byte[] { } : Encoding.Default.GetBytes(obj.ToString()); ;
        }

    }
}
